package com.youfang.sixthmedia.message;

import java.io.Serializable;

import org.json.JSONArray;
import org.json.JSONObject;

import com.youfang.sixthmedia.controller.LogoController;
import com.youfang.sixthmedia.controller.PhotoController;
import com.youfang.sixthmedia.message.model.DetailBuilding;
import com.youfang.sixthmedia.message.model.DetailBuilding.PhotoItem;
import com.youfang.sixthmedia.net.Response;

/**
 * 获取新楼房列表请求
 * 
 * @author Administrator
 * 
 */
public class GethousedetailResponse extends Response implements Serializable {

	private DetailBuilding detail = new DetailBuilding();

	public static void parse(DetailBuilding detail, JSONObject bodyJSON)
			throws Exception {
		JSONObject o = bodyJSON;

		if (o.has("distance")) {
			detail.distance = o.getDouble("distance");
		}

		detail.id = o.getString("id");
		detail.buildingname = o.getString("buildingname");
		detail.logo = o.getString("logo");
		detail.readnum = o.getString("readnum");
		detail.price = o.getString("price");
		detail.address = o.getString("address");

		detail.privilegeid = getInt(o, "privilegeid");

		detail.longitude = o.getDouble("longitude");
		detail.latitude = o.getDouble("latitude");

		detail.grouponbydev = getInt(o, "grouponbydev");

		detail.pricedesc = o.getString("pricedesc");
		detail.advisername = getString(o, "advisername");
		detail.developername = getString(o, "developername");

		detail.buildingtype = o.getString("buildingtype");
		detail.region = o.getString("region");

		// unix timestamp to java time
		detail.opentime = getLong(o, "opentime") * 1000;

		detail.special = getString(o, "special");
		detail.surroundbusi = getString(o, "surroundbusi");
		detail.Surroundtraffic = getString(o, "Surroundtraffic");

		// 顾问信息...
		detail.Consultantname = getString(o, "Consultantname");
		detail.Consultantemail = getString(o, "Consultantemail");
		detail.Consultantphone = getString(o, "Consultantphone");
		detail.isverify = getString(o, "isverify");

		detail.adviserlevel = getString(o, "adviserlevel");
		detail.introduce = getString(o, "introduce");
		detail.photourl = getString(o, "photourl");
		detail.dev = getString(o, "dev");
		detail.gratiamsg = getString(o, "gratiamsg");
		detail.privilegedesc = getString(o, "privilegedesc");
		detail.buildingdetail = getString(o, "buildingdetail");

		detail.iscollect = getString(o, "iscollect");
		detail.isgroupon = getString(o, "isgroupon");

		if (o.has("photos")) {
			JSONArray photoJSON = o.getJSONArray("photos");
			detail.photos = new String[photoJSON.length()];
			for (int i = 0; i < photoJSON.length(); i++) {
				detail.photos[i] = photoJSON.getJSONObject(i).getString("url");
				PhotoItem item = new PhotoItem();
				item.url = detail.photos[i];
				item.localURI = PhotoController.Cache.getFromCacheURI(item.url);
				detail.items.add(item);
			}
		}

		// 内部标示的ID
		detail.innerID = Long.parseLong(detail.id);
		detail.logoBitmap = LogoController.Cache.getFromCache(detail);

	}

	@Override
	protected void decodeBody(JSONObject bodyJSON) throws Exception {

		parse(this.detail, bodyJSON);
	}

	public DetailBuilding getDetail() {
		return detail;
	}

	public void setDetail(DetailBuilding detail) {
		this.detail = detail;
	}

}
